package com.cuit.eduservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cuit.eduservice.entity.EduTeacher;
import com.cuit.eduservice.entity.vo.TeacherQuery;
import com.cuit.eduservice.service.EduTeacherService;
import com.cuit.exception.MyException;
import com.cuit.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author horizon
 * @since 2020-09-12
 */
@Api(description = "教师管理")
@RestController
@RequestMapping("/eduService/teacher")
@CrossOrigin //解决跨域
public class EduTeacherController {

    @Autowired
    EduTeacherService eduTeacherService;

    @ApiOperation("教师列表")
    @GetMapping("/findAll")
    public R findAll(){
        List<EduTeacher> list = eduTeacherService.list(null);
        //try {
        //    int i=1/0;
        //}catch (Exception e){
        //    //执行自定义异常
        //    throw new MyException(20001,"执行了自定义异常");
        //}
        return R.ok().data("items",list);
    }

    //浏览器不能用delete提交
    @ApiOperation("逻辑删除教师")
    @DeleteMapping("/logicDelete/{id}")
    public R logicDelete(@ApiParam(name="id",value = "教师Id",required = true) @PathVariable("id") String id){
        boolean flag = eduTeacherService.removeById(id);
        if(flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @ApiOperation("分页查询")
    @GetMapping("/page/{current}/{limit}")
    public R PageList(@PathVariable("current") Long current,@PathVariable("limit") Long limit){
        Page<EduTeacher> pageTeacher=new Page<>(current,limit);
        eduTeacherService.page(pageTeacher,null);
        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("records",records);
        return R.ok().data(map);
        //return R.ok().data("total",total).data("records",records);
    }

    //@RequestBody 需要post提交
    @ApiOperation("多条件查询")
    @PostMapping("/pageCondition/{current}/{limit}")
    public R pageCondition(@PathVariable("current") Long current,
                           @PathVariable("limit") Long limit,
                           @RequestBody(required = false) TeacherQuery teacherQuery){
        //创建page对象
        Page<EduTeacher> pageTeacher=new Page<>(current,limit);
        QueryWrapper<EduTeacher> wrapper=new QueryWrapper<>();
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)){
            wrapper.le("gmt_create",end);
        }
        eduTeacherService.page(pageTeacher,wrapper);
        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();
        return R.ok().data("total",total).data("records",records);
    }

    @ApiOperation("添加教师")
    @PostMapping("/addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean save = eduTeacherService.save(eduTeacher);
        if(save){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @ApiOperation("获取教师")
    @GetMapping("/getTeacher/{id}")
    public R getTeacher(@PathVariable("id") String id){
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        return R.ok().data("teacher",eduTeacher);
    }

    @ApiOperation("修改教师")
    @PostMapping("/updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean flag = eduTeacherService.updateById(eduTeacher);
        if(flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

}

